package gui;

import io.ItemAgenda;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Mariano Zapata
 */
public class DialogoAgendaNota extends javax.swing.JPanel {

    private ItemAgenda item;
    
    private static final int MAX_CARACTERES= 300;
    private static final String AVISO_ELIMINADO= "Este recordatorio ha sido eliminado por otro usuario en la red.";
    
    /** Creates new form DialogoAgendaNota */
    public DialogoAgendaNota(ItemAgenda i)
    {
        initComponents();
        Image icono= new ImageIcon(getClass().getResource("/gui/icono/logo_chico.png")).getImage();
        dialogo.setIconImage(icono);
        dialogo.getContentPane().setLayout(new BorderLayout());
        caracteres.setText(String.valueOf(MAX_CARACTERES));
        TextoPlanoLimitado tpl= new TextoPlanoLimitado(MAX_CARACTERES, caracteres);
        texto.setDocument(tpl);
        item= i;
        fecha.setText(i.getFecha());
        if (!i.getHora().isEmpty())
        {
            int [] h= this.transformarHora(i.getHora());
            hora.setValue(h[0]);
            minutos.setValue(h[1]);
        }
        esImportante.setSelected(i.getEsImportante());
        texto.setText(i.getTexto());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dialogo = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        fecha = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        hora = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        minutos = new javax.swing.JSpinner();
        caracteres = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        texto = new javax.swing.JTextArea();
        guardar = new javax.swing.JButton();
        esImportante = new javax.swing.JCheckBox();
        jSeparator1 = new javax.swing.JSeparator();

        dialogo.setTitle("Recordatorio");
        dialogo.setModal(true);
        dialogo.setResizable(false);
        dialogo.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                cruz(evt);
            }
        });

        javax.swing.GroupLayout dialogoLayout = new javax.swing.GroupLayout(dialogo.getContentPane());
        dialogo.getContentPane().setLayout(dialogoLayout);
        dialogoLayout.setHorizontalGroup(
            dialogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        dialogoLayout.setVerticalGroup(
            dialogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jLabel1.setText("Fecha:");

        fecha.setEditable(false);
        fecha.setFont(new java.awt.Font("Tahoma", 1, 11));

        jLabel2.setText("Hora:");

        hora.setModel(new javax.swing.SpinnerNumberModel(0, 0, 23, 1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText(":");

        minutos.setModel(new javax.swing.SpinnerNumberModel(0, 0, 59, 1));

        caracteres.setEditable(false);

        jLabel4.setText("Caracteres restantes:");

        jLabel5.setText("Nota:");

        texto.setColumns(20);
        texto.setLineWrap(true);
        texto.setRows(5);
        texto.setWrapStyleWord(true);
        jScrollPane1.setViewportView(texto);

        guardar.setText("Guardar");
        guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardar(evt);
            }
        });

        esImportante.setText("Evento importante");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(hora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(minutos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 143, Short.MAX_VALUE)
                        .addComponent(esImportante))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 314, Short.MAX_VALUE)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(caracteres, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(guardar, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(hora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(minutos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(esImportante))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(caracteres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(guardar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void guardar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardar
        String msj= "No ha ingresado ninguna nota.";
        String s= this.corregirTexto(texto.getText());
        if (s.isEmpty())
            JOptionPane.showMessageDialog(this, msj, "Error", JOptionPane.ERROR_MESSAGE);
        else
        {
            item.setHora(this.transformarHora((Integer) hora.getValue(), (Integer) minutos.getValue()));
            item.setEsImportante(esImportante.isSelected());
            item.setTexto(s);
            dialogo.setVisible(false);
        }
    }//GEN-LAST:event_guardar

    private void cruz(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_cruz
        item= null;
    }//GEN-LAST:event_cruz

    public ItemAgenda hacerVisible(Component c)
    {
        dialogo.getContentPane().add(this, BorderLayout.CENTER);
        dialogo.pack();
        dialogo.setLocationRelativeTo(c);
        dialogo.setVisible(true);
        dialogo.dispose();
        return item;
    }
    
    public ItemAgenda getItemAbierto() {
        return item;
    }
    
    public void forzarCierre()
    {
        item= null;
        JOptionPane.showMessageDialog(this, AVISO_ELIMINADO, "Aviso", JOptionPane.WARNING_MESSAGE);
        dialogo.setVisible(false);
    }
    
    private int [] transformarHora(String hora)
    {
        String [] celula= hora.split(":");
        int [] ret= new int[2];
        ret[0]= Integer.valueOf(celula[0]);
        ret[1]= Integer.valueOf(celula[1]);
        return ret;
    }
    
    private String transformarHora(int h, int m)
    {
        String ret;
        if (h < 10)
            ret= "0" + h + ":";
        else
            ret= h + ":";
        if (m < 10)
            ret+= "0" + m;
        else
            ret+= m;
        return ret;
    }

    private String corregirTexto(String s)
    {
        String temp= s.trim();
        int size= temp.length();
        int quitar= 0;
        for (int k= 0; k < size; k++)
        {
            if (temp.charAt(k) == '\n')
                quitar+= 1;
            else
                break;
        }
        if (quitar > 0)
            return temp.substring(quitar);
        else
            return temp;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField caracteres;
    private javax.swing.JDialog dialogo;
    private javax.swing.JCheckBox esImportante;
    private javax.swing.JTextField fecha;
    private javax.swing.JButton guardar;
    private javax.swing.JSpinner hora;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSpinner minutos;
    private javax.swing.JTextArea texto;
    // End of variables declaration//GEN-END:variables
}
